package zoschina.app.ivan.starweather.util;

import android.content.Context;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import zoschina.app.ivan.starweather.model.City;
import zoschina.app.ivan.starweather.model.Weather;
import zoschina.app.ivan.starweather.model.WeatherInfo;

/**
 * Created by Ivan on 2015/8/24.
 */
public class HttpUtil {

    private static final String apikey="d1b8f94688ce775bf6b8755a661d2e9d";

    public static void sendHttpRequest(final String address,final HttpCallbackListener listener){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection=null;
                try {
                    URL url=new URL(address);
                    connection=(HttpURLConnection)url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream in=connection.getInputStream();
                    BufferedReader reader=new BufferedReader(new InputStreamReader(in));
                    StringBuilder response=new StringBuilder();
                    String line;
                    while ((line=reader.readLine())!=null){
                        response.append(line);
                    }

                    if(listener!=null){
                        listener.onFinish(response.toString());
                    }
                } catch (Exception e) {
                    if(listener!=null){
                        listener.onError(e);
                    }
                }finally {
                    if(connection!=null){
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }

    public static void sendBaiduWeatherHttpRequest(final String address,final HttpCallbackListener listener){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection=null;
                try {
                    URL url=new URL(address);
                    connection=(HttpURLConnection)url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setRequestProperty("apikey", apikey);
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    connection.connect();
                    InputStream in=connection.getInputStream();
                    BufferedReader reader=new BufferedReader(new InputStreamReader(in));
                    StringBuilder response=new StringBuilder();
                    String line;
                    while ((line=reader.readLine())!=null){
                        response.append(line);
                    }

                    if(listener!=null){
                        listener.onFinish(response.toString());
                    }
                } catch (Exception e) {
                    if(listener!=null){
                        listener.onError(e);
                    }
                }finally {
                    if(connection!=null){
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }

    public static String handleIpAddressResponse(Context context,String response) {
        try{
            JSONObject jsonObject=new JSONObject(response);
            JSONObject retData=jsonObject.getJSONObject("data");
            return retData.getString("ip");

        }catch (JSONException e){
            e.printStackTrace();
        }

        return null;
    }

    public static void handleWeatherResponse(Context context,String response) {
        try{
            JSONObject jsonObject=new JSONObject(response);
            JSONObject retBody=jsonObject.getJSONObject("showapi_res_body");

            JSONObject cityInfo=retBody.getJSONObject("cityInfo");
            City city=new City(cityInfo.getString("c5"),"");

            Weather today=new Weather();
            JSONObject nowInfo=retBody.getJSONObject("now");
            today.setCurTemp(nowInfo.getString("temperature"));
            today.setDesp(nowInfo.getString("weather"));
            JSONObject todayInfo=retBody.getJSONObject("f1");
            today.setHighTemp(todayInfo.getString("day_air_temperature"));
            today.setLowTemp(todayInfo.getString("night_air_temperature"));

            WeatherInfo weatherInfo=new WeatherInfo();
            weatherInfo.setCity(city);
            weatherInfo.setToday(today);

            Utility.saveWeatherInfo(context, weatherInfo);
        }catch (JSONException e){
            e.printStackTrace();
        }
    }



}
